package og.example.views.group;
import og.example.entity.Group;
import og.example.service.GroupService;
import og.example.views.MainFrame;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import javax.swing.table.DefaultTableModel;
public class GroupListFrame extends JFrame {
    private JTable groupTable;

    public GroupListFrame() {
        setTitle("小组列表");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        String[] columnNames = {"小组ID", "小组名称", "班级名称", "学生数量"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);

        List<Group> groups = new GroupService().groupQuery();
        for (Group group : groups) {
            Object[] row = {group.getGroupId(), group.getGroupName(), group.getClassName(), group.getStudentCount()};
            tableModel.addRow(row);
        }

        groupTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(groupTable);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        JButton backButton = new JButton("返回主界面");

        buttonPanel.add(backButton);

        backButton.addActionListener(e -> {
            dispose(); // 关闭当前窗口
            new MainFrame(); // 打开主界面
        });

        getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }
}